package com.gitee.attendance.controller;

import java.io.FileInputStream;

import javax.servlet.http.HttpServletRequest;

import com.gitee.attendance.entity.User;
import com.gitee.attendance.service.UserServiceI;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

@Controller
@RequestMapping("/user")
public class UserController {

    private Logger logger = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private UserServiceI userService;

    @RequestMapping("/add")
    public String  addUser() throws Exception {
        return "add";
    }
    
    @ResponseBody
    @RequestMapping("/addUser")
    public String addUser(HttpServletRequest request, @RequestParam("file") MultipartFile multipartFile)
            throws Exception {
        String userName = request.getParameter(User.USERNAME);
        String userCode = request.getParameter(User.USERCODE);
        FileInputStream fis = (FileInputStream) multipartFile.getInputStream();

        User user = new User();
        user.setUserName(userName);
        user.setUserCode(userCode);
        user.setUserFace(fis);
        String result = userService.addUser(user);
        logger.info(result);
        return result;
    }

}